package com.backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution09 {
    List<List<String>> res = new ArrayList<>();


    public List<List<String>> partition(String s) {
        backTracking(s,0);
        return res;
    }

    LinkedList<String> tmp = new LinkedList<>();
    public void backTracking(String s, int index) {
        if (index >= s.length()) {
            res.add(new ArrayList<>(tmp));
            return;
        }

        for (int i = index; i < s.length(); i++) {
            if (isPalindrome(s,index,i)) {
                tmp.add(s.substring(index,i+1));
            } else continue;
            backTracking(s,i+1);
            tmp.removeLast();
        }
    }

    public boolean isPalindrome(String str,int start,int end) {
        for (int i = start,j = end; i <j ; i++,j--) {
            if (str.charAt(i) != str.charAt(j)) return false;
        }
        return true;
    }
}
